#include  	"main.h"
#include 		"system.h"

void HC06_Test(void)
{
	uint8	*p="AT";
	uint8	len=strlen(p),i=5;

    printf("--->>>HC06 TEST\n");
	
    while(i--)
    {
    	
        Bluetooth_ReceData->len=0 ;
        HAL_UART_Transmit(&BluetoothUart,p,len, 0xFFFF);
        HAL_Delay(1000);
        printf("len=%d\n",Bluetooth_ReceData->len);
        Bluetooth_ReceData->buf[Bluetooth_ReceData->len]=0 ;
        printf("%s\n",Bluetooth_ReceData->buf);

        
        if(strstr(Bluetooth_ReceData->buf,"OK")!=NULL)
        {
            printf("HC06 is OK\n");
            return ;
        }
        
    }

	printf("HC06 TEST ERR\n");
    
}

void HC06_ChangeName(void)
{
	uint8	*p="AT+NAMETuring";
	uint8	len=strlen(p),i=5;

    printf("--->>>HC06 ChangeName\n");
    while(i--)
    {
        Bluetooth_ReceData->len=0 ;
        HAL_UART_Transmit(&BluetoothUart,p,len, 0xFFFF);
        HAL_Delay(1000);
        printf("len=%d\n",Bluetooth_ReceData->len);
        Bluetooth_ReceData->buf[Bluetooth_ReceData->len]=0 ;
		Bluetooth_ReceData->len=0;
        printf("%s\n",Bluetooth_ReceData->buf);

        
        if(strstr(Bluetooth_ReceData->buf,"OKsetname")!=NULL)
        {
            printf("HC06 ChangeName OK\n");
            return ;
        }
        
    }
	printf("HC06 ChangeName ERR\n"); 
}

void HC06_SetPassWord(void)
{
	uint8	*p="AT+PIN6666";
	uint8	len=strlen(p),i=5;

    printf("--->>>HC06 SetPassWord\n");
    while(i--)
    {
        Bluetooth_ReceData->len=0 ;
        HAL_UART_Transmit(&BluetoothUart,p,len, 0xFFFF);
        HAL_Delay(1000);
        printf("len=%d\n",Bluetooth_ReceData->len);
        Bluetooth_ReceData->buf[Bluetooth_ReceData->len]=0 ;
		Bluetooth_ReceData->len=0;
        printf("%s\n",Bluetooth_ReceData->buf);

        
        if(strstr(Bluetooth_ReceData->buf,"OKsetPIN")!=NULL)
        {
            printf("HC06 SetPassWord OK\n");
            return ;
        }
        
    }
	printf("HC06 SetPassWord ERR\n"); 
}

void HC06_ReceiveData(void)
{
	uint8	i=0;
	uint16	j=0;

	if(Bluetooth_ReceData->len>0)
	{
		if (strncmp(Bluetooth_ReceData->buf,Cmd1Header,strlen(Cmd1Header)) == 0&&\
		    strstr(Bluetooth_ReceData->buf, "OK")!=NULL )
		{

			Bluetooth_ReceData->processed=0;�������ݡ�
			printf("len=%d\n",Bluetooth_ReceData->len);
			Bluetooth_ReceData->buf[Bluetooth_ReceData->len]=0 ;
			printf("%s\n",Bluetooth_ReceData->buf);

			SysPara.thresholdVal.TemThreshold_a =UtilGetIntFlag(Bluetooth_ReceData->buf, "TThred_a:");
			SysPara.thresholdVal.TemThreshold_b =UtilGetIntFlag(Bluetooth_ReceData->buf, "TThred_b:");
			SysPara.thresholdVal.TemThreshold_c =UtilGetIntFlag(Bluetooth_ReceData->buf, "TThred_c:");
			SysPara.thresholdVal.TemThreshold_s =UtilGetIntFlag(Bluetooth_ReceData->buf, "TThred_s:");
		
			SysPara.thresholdVal.Vol_OverThreshold_a=UtilGetIntFlag(Bluetooth_ReceData->buf, "VOverThred_a:");
			SysPara.thresholdVal.Vol_OverThreshold_b=UtilGetIntFlag(Bluetooth_ReceData->buf, "VOverThred_b:");
			SysPara.thresholdVal.Vol_OverThreshold_c=UtilGetIntFlag(Bluetooth_ReceData->buf, "VOverThred_c:");

			SysPara.thresholdVal.Vol_BelowThreshold_a=UtilGetIntFlag(Bluetooth_ReceData->buf, "VBelowThred_a:");
			SysPara.thresholdVal.Vol_BelowThreshold_b=UtilGetIntFlag(Bluetooth_ReceData->buf, "VBelowThred_b:");
			SysPara.thresholdVal.Vol_BelowThreshold_c=UtilGetIntFlag(Bluetooth_ReceData->buf, "VBelowThred_c:");

			
			SysPara.thresholdVal.CurThreshold_a=UtilGetIntFlag(Bluetooth_ReceData->buf, "CThred_a:");
			SysPara.thresholdVal.CurThreshold_b=UtilGetIntFlag(Bluetooth_ReceData->buf, "CThred_b:");
			SysPara.thresholdVal.CurThreshold_c=UtilGetIntFlag(Bluetooth_ReceData->buf, "CThred_c:");
			SysPara.thresholdVal.CurThreshold_r=UtilGetIntFlag(Bluetooth_ReceData->buf, "CThred_r:");
		
			SysPara.thresholdVal.PowerThreshold=UtilGetIntFlag(Bluetooth_ReceData->buf, "PowerThred:");

			printf("--->>>����ֵ\nC=%d ",SysPara.thresholdVal.CurThreshold_a);
			printf("%d ",SysPara.thresholdVal.CurThreshold_b);	
			printf("%d ",SysPara.thresholdVal.CurThreshold_c);	
			printf("%d\n",SysPara.thresholdVal.CurThreshold_r); 		
			
			printf("V=%d ",SysPara.thresholdVal.Vol_OverThreshold_a);
			  printf("%d ",SysPara.thresholdVal.Vol_OverThreshold_b);	
			 printf("%d\n",SysPara.thresholdVal.Vol_OverThreshold_c);	
			
			  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_a);
			  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_b);
			 printf("%d\n",SysPara.thresholdVal.Vol_BelowThreshold_c);
			
			printf("T=%d ",SysPara.thresholdVal.TemThreshold_a);
			printf("%d ",SysPara.thresholdVal.TemThreshold_b);
			printf("%d ",SysPara.thresholdVal.TemThreshold_c);
			printf("%d\n",SysPara.thresholdVal.TemThreshold_s); 


			
			i=0;
			while(++i<4)
			{
				printf("send=%d\n",Bluetooth_ReceData->len);
				SendDataToCollectBoard(Bluetooth_ReceData->buf,Bluetooth_ReceData->len);
				HAL_Delay(3000);

				if(CollectBoard_ReceData->len>0)
				{
					CollectBoard_ReceData->buf[CollectBoard_ReceData->len]=0;
					printf("-->>%s\n",CollectBoard_ReceData->buf);

					
					for(j=0;j<CollectBoard_ReceData->len;j++)
					{
						printf("%c",CollectBoard_ReceData->buf[j]);
					}

					printf("\nlen-->>%d\n",CollectBoard_ReceData->len);
					#if 1
					if(UtilFindStringContent(CollectBoard_ReceData->buf,CollectBoard_ReceData->len,Cmd1SuccessAck)==TRUE)
					{
						CollectBoard_ReceData->len=0;
						break;
					}
					#else
				 	if (strstr(CollectBoard_ReceData->buf,Cmd1SuccessAck)!=NULL )
				 	{
				 		CollectBoard_ReceData->len=0;
						break;
					}
					#endif
				}

			}

			memset(Bluetooth_ReceData->buf,0,Uart_ReceBufLen);
			Bluetooth_ReceData->len=0;
			Bluetooth_ReceData->processed=1;��ʼ��������
			if(i<4)	
			{
				printf("-->>save data\n");
				SysPara.thresholdVal_UsedDefault=Not_UsedDefaultValue;
				SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));

				/*SysPara.thresholdVal.TemThreshold_a=SysPara.thresholdVal.TemThreshold_a*10;
				SysPara.thresholdVal.TemThreshold_b=SysPara.thresholdVal.TemThreshold_b*10;
				SysPara.thresholdVal.TemThreshold_c=SysPara.thresholdVal.TemThreshold_c*10;
				SysPara.thresholdVal.TemThreshold_s=SysPara.thresholdVal.TemThreshold_s*10;
				
				
				SysPara.thresholdVal.CurThreshold_a=SysPara.thresholdVal.CurThreshold_a*1000;
				SysPara.thresholdVal.CurThreshold_b=SysPara.thresholdVal.CurThreshold_b*1000;
				SysPara.thresholdVal.CurThreshold_c=SysPara.thresholdVal.CurThreshold_c*1000;
				SysPara.thresholdVal.CurThreshold_r=SysPara.thresholdVal.CurThreshold_r*1000;*/

			
			}

		
		}
		else if (strncmp(Bluetooth_ReceData->buf,Cmd3Header,strlen(Cmd3Header)) == 0&&\
		    strstr(Bluetooth_ReceData->buf, "OK")!=NULL )
		{
			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "CurChannle_a:")==1)
			{
				printf("Ca\n");
				SysPara.ChannleStatus.CurChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_a=Channle_Status_Close;
			}


			
			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "CurChannle_b:")==1)
			{
				printf("Cb\n");
				SysPara.ChannleStatus.CurChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_b=Channle_Status_Close;
			}

			
			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "CurChannle_c:")==1)
			{
				printf("Cc\n");
				SysPara.ChannleStatus.CurChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_c=Channle_Status_Close;
			}

			
			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "CurChannle_r:")==1)
			{
				printf("Cr\n");
				SysPara.ChannleStatus.CurChannle_r=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_r=Channle_Status_Close;
			}	




			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "VolChannle_a:")==1)
			{
				printf("Va\n");
				SysPara.ChannleStatus.VolChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_a=Channle_Status_Close;
			}	

			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "VolChannle_b:")==1)
			{
				printf("Vb\n");
				SysPara.ChannleStatus.VolChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_b=Channle_Status_Close;
			}	

			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "VolChannle_c:")==1)
			{
				printf("Vc\n");
				SysPara.ChannleStatus.VolChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_c=Channle_Status_Close;
			}				

			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "TemChannle_a:")==1)
			{
				printf("Ta\n");
				SysPara.ChannleStatus.TemChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_a=Channle_Status_Close;
			}


			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "TemChannle_b:")==1)
			{
				printf("Tb\n");
				SysPara.ChannleStatus.TemChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_b=Channle_Status_Close;
			}


			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "TemChannle_c:")==1)
			{
				printf("Tc\n");
				SysPara.ChannleStatus.TemChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_c=Channle_Status_Close;
			}


			if(UtilGetIntFlag(Bluetooth_ReceData->buf, "TemChannle_s:")==1)
			{
				printf("Ts\n");
				SysPara.ChannleStatus.TemChannle_s=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_s=Channle_Status_Close;
			}

			printf("--->>>ͨ������\nC=%02x ",SysPara.ChannleStatus.CurChannle_a);
			printf("%02x ",SysPara.ChannleStatus.CurChannle_b);
			printf("%02x ",SysPara.ChannleStatus.CurChannle_c);
			printf("%02x\n",SysPara.ChannleStatus.CurChannle_r);
			
			printf("V=%02x ",SysPara.ChannleStatus.VolChannle_a);
			printf("%02x ",SysPara.ChannleStatus.VolChannle_b);
			printf("%02x\n",SysPara.ChannleStatus.VolChannle_c);
			
			printf("T=%02x ",SysPara.ChannleStatus.TemChannle_a);
			printf("%02x ",SysPara.ChannleStatus.TemChannle_b);
			printf("%02x ",SysPara.ChannleStatus.TemChannle_c);
			printf("%02x\n",SysPara.ChannleStatus.TemChannle_s);


			
			i=0;
			while(++i<4)
			{
				printf("send=%d\n",Bluetooth_ReceData->len);
				SendDataToCollectBoard(Bluetooth_ReceData->buf,Bluetooth_ReceData->len);
				HAL_Delay(3000);
			
				if(CollectBoard_ReceData->len>0)
				{
					CollectBoard_ReceData->buf[CollectBoard_ReceData->len]=0;
					printf("-->>%s\n",CollectBoard_ReceData->buf);
			
					
					for(j=0;j<CollectBoard_ReceData->len;j++)
					{
						printf("%c",CollectBoard_ReceData->buf[j]);
					}
			
					printf("\nlen-->>%d\n",CollectBoard_ReceData->len);
		
					if(UtilFindStringContent(CollectBoard_ReceData->buf,CollectBoard_ReceData->len,Cmd3SuccessAck)==TRUE)
					{
						CollectBoard_ReceData->len=0;
						break;
					}
	
				}
			
			}

			memset(Bluetooth_ReceData->buf,0,Uart_ReceBufLen);
			Bluetooth_ReceData->len=0;
			Bluetooth_ReceData->processed=1;��ʼ��������
			if(i<4) 
			{
				printf("<<<---->>save data\n");
				SysPara.ChannleStatus_UsedDefault=Not_UsedDefaultValue;
				SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));
			}

		}

	}
}


void SysParaSave(uint32 saveaddress,uint8 *buff,uint8 len)
{
	uint8  i=0;
	HAL_FLASH_Unlock();
	
	__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
	FLASH_PageErase(saveaddress);
	FLASH_WaitForLastOperation(1000);
	CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

	for(i=0;i<len;i=i+2)
	{
		
		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,saveaddress+i,buff[i+1]<<8|buff[i]);
	   
	}
	HAL_FLASH_Lock();
	/*
	printf("%02x\n",*(__IO uint8_t*)(saveaddress));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+1));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+2));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+3));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+4));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+5));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+6));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+7));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+8));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+9));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+10));
	printf("%02x\n",*(__IO uint8_t*)(saveaddress+11));
	*/


}
